package com.aquan.map_jetpack.ui.screens

import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Phone
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.rememberNavController
import com.aquan.map_jetpack.model.entity.NavigationItem

@Composable
fun MainFrame(onNavigateTo: () -> Unit = {}, onNavigateToLogin: () -> Unit = {}) {

    val navigationItems = listOf(
        NavigationItem(title = "首页", icon = Icons.Filled.Home),
        NavigationItem(title = "新闻", icon = Icons.Filled.Phone),
        NavigationItem(title = "我的", icon = Icons.Filled.Settings)
    )

    var currentIndex by remember {
        mutableStateOf(1)
    }

    val navController = rememberNavController()
    val save = rememberSaveableStateHolder()


    Box(Modifier.navigationBarsPadding()) {
        Scaffold(bottomBar = {
            BottomNavigation() {
                navigationItems.forEachIndexed { index, navigationItem ->
                    BottomNavigationItem(
                        selected = currentIndex == index,
                        onClick = {
                            currentIndex = index
                        },
                        icon = {
                            Icon(imageVector = navigationItem.icon, contentDescription = null)
                        }, label = {
                            Text(text = navigationItem.title)
                        })
                }
            }
        },
            content = {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(bottom = 56.dp)
                ) {


                    when (currentIndex) {
                        0 -> save.SaveableStateProvider(key = true) {
                            HomeScreen()
                        }
                        1 -> save.SaveableStateProvider(key = true) {
                            NewsScreen(
                                onNavigateTo = {
                                    Log.i("+++", "新闻界面")
                                    onNavigateTo.invoke()
                                }
                            )
                        }
                        2 -> save.SaveableStateProvider(key = true) {
                            SettingScreen(
                                onNavigateTo = {
                                    onNavigateToLogin.invoke()
                                }
                            )
                        }
                    }
                }
            }
        )
    }
}

@Preview(showBackground = true)
@Composable
fun MainFramePreView() {
    MainFrame()
}